<?php
	namespace app\advert\admin;

	use app\admin\controller\Admin;
	use app\common\builder\ZBuilder;
	use think\Db;

	class Menu extends Admin
	{
		public function index(){

			$items = Db::name('advert')->select();
			
			return 	ZBuilder::make('table')
					->addColumns([
						['id','广告编号','callback',function($value){
							return '#Nav-'.$value;
						}],
						['group','所属分组','callback',function($value){
							$item = Db::name('advert_group')->where('id',$value)->find();
							return $item['name'];
						}],
						['name','显示内容','callback',function($value,$row){
							$img = Db::name('admin_attachment')->where('id',$row['imgsrc'])->column('path');
							if( $img && '2' == $row['type']){
								return "<img width='50' height='40' src='".$img[0]."' alt='".$row['alt']."' />";
							}else{
								return $value;
							}
							
						}, '__data__'],
						['links','对应链接','callback',function($value){
							return $value . " <a title='前往查看' href='".$value."'><i class='fa fa-fw fa-location-arrow'></i></a>";
						}],
						['type','广告类型','callback',function($value){
							$list = ['代码','文字','图片','视频'];
							return $list[$value];
						}],
						['hassub','包含下级','callback',function($value){
							return $value == 0?'无':'有';
						}],
						['right_button','操作'],
					])
					->addTopButton('add') // 批量添加顶部按钮
					->addRightButtons(['edit','delete'])
					->setRowList($items)
					->fetch();
		}

		//添加广告
		public function add(){

			if($this->request->isPost()){
				$data = $this->request->post();
				$id = Db::name('advert')->insertGetid($data);
				if($id){
					if( '' != $data['parentid'] ){
						$item = [];
						$item['hassub'] = 1;
						Db::name('advert')->where('id',$data['parentid'])->update($item);
					}
					return $this->success('添加成功',url('advert/menu/index'));
				}else
				{
					return $this->error('添加失败',url('advert/advert/add'));
				}
			}else
			{
				$group = Db::name('advert_group')->column('id,name');	
				$advs = Db::name('advert')->column('id,name');
				
				return 	ZBuilder::make('form')
						->addFormItems([
			                ['select', 'group', '广告分类', '', $group, 0],
			                ['select', 'parentid', '上级节点', '没有上级节点不选', $advs],
			                ['radio', 'type', '广告类型', '', ['代码', '文字', '图片', '视频'], 1],
			                ['text', 'name', '文字内容', '<code>必填</code>'],
			                ['image', 'imgsrc', '图片', '<code>必须</code>'],
			                ['text', 'links', '链接', '<code>必填</code>'],
			                ['text', 'size', '文字大小', '只需填写数字，例如:12，表示12px', '',  ['', 'px']],
			                ['text', 'width', '宽度', '不用填写单位，只需填写具体数字'],
			                ['text', 'height', '高度', '不用填写单位，只需填写具体数字'],
			                ['text', 'alt', '图片描述', '即图片alt的值'],
			                ['textarea', 'code', '代码', '<code>必填</code>，支持html代码'],
			                ['radio', 'status', '立即启用', '', ['否', '是'], 1]
			            ])
			            ->setTrigger('type', '0', 'code')
			            ->setTrigger('type', '1', 'size')
			            ->setTrigger('type', '1,2,3', 'name,links')
			            ->setTrigger('type', '2', 'imgsrc,alt')
			            ->setTrigger('type', '2,3', 'width,height')
			            ->layout(['group'=>4,'parentid'=>4,'type'=>4])
			            ->fetch();
			}
		}
	}
	